/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.Tesk;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.domain.vo.TeskQueryCriteria;
import me.zhengjie.modules.system.mapper.TeskMapper;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.modules.system.service.TeskService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Security;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author shitou
* @date 2024-05-23
**/
@Service
@RequiredArgsConstructor
public class TeskServiceImpl extends ServiceImpl<TeskMapper, Tesk> implements TeskService {

    private final TeskMapper teskMapper;

    private final UserMapper userMapper;

    @Override
    public PageResult<Tesk> queryAll(TeskQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(teskMapper.findAll(criteria, page));
    }

    @Override
    public List<Tesk> queryAll(TeskQueryCriteria criteria){
        return teskMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Tesk resources) {
        User user = userMapper.selectById(SecurityUtils.getCurrentUserId());
        resources.setSchoolId(Math.toIntExact(user.getDeptId()));
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Tesk resources) {
        User user = userMapper.selectById(SecurityUtils.getCurrentUserId());
        Tesk tesk = getById(resources.getId());
        tesk.setSchoolId(Math.toIntExact(user.getDeptId()));
        tesk.copy(resources);
        saveOrUpdate(tesk);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Tesk> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Tesk tesk : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" name",  tesk.getName());
            map.put(" createtime",  tesk.getCreatetime());
            map.put(" endtime",  tesk.getEndtime());
            map.put(" type",  tesk.getType());
            map.put(" info",  tesk.getInfo());
            map.put(" lastupdatetime",  tesk.getLastupdatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}